<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.PreparedStatement" %><%--
  Created by IntelliJ IDEA.
  User: LH
  Date: 2022/9/30
  Time: 16:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //1.加载驱动程序
    Class.forName("com.mysql.cj.jdbc.Driver");
    //2.建立与数据库的连接
    String url = "jdbc:mysql://localhost:3306/javaeelab";
    Connection conn = DriverManager.getConnection(url, "root", "root");
    //3.创建命令对象

    String sql="insert into tb_major values(?,?)";
    PreparedStatement ps = conn.prepareStatement(sql);
    String name = request.getParameter("name");
    String id = request.getParameter("id");
    ps.setString(1,id);
    ps.setString(2,name);
    int i = ps.executeUpdate();
    System.out.println(i);
    if (i==1){
        request.getRequestDispatcher("showAllMajors.jsp").forward(request,response);
    }else{
        out.print("添加失败");
    }
%>
</body>
</html>
